package de.hauck.rtmidlet.views;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.utils.MyLogger;

public class FormLinks extends Form implements CommandListener {
	
	private RTMidlet _main;
	private FormSettingsMenu _fm;
	private Command cSave = new Command("Save", Command.OK, 1);
	private Command cAddLink = new Command("Add Link...", Command.OK, 2);
	private Command cDelLink = new Command("Delete Link...", Command.OK, 3);
	private Command cCancel = new Command("Cancel", Command.BACK, 1);
	//private TextField url1, url2, url3,url4;
	private TextField url5,link1, link2;
	private TextField[] chartUrls;
	//private TextField
	
	public FormLinks(RTMidlet rtmidlet, FormSettingsMenu fm) {
		super("Link Settings");
		_main=rtmidlet;
		_fm=fm;
		String[] chartType=Configuration.getAvailableChartTypes();
		chartUrls=new TextField[chartType.length];
		for (int i=0; i<chartUrls.length; i++) {
			chartUrls[i]=new TextField("URL "+chartType[i],Configuration.CHART_URLS[i],255,0);
			append(chartUrls[i]);
		}
//		url1=new TextField("URL RT", Configuration.urlRT, 255, 0);
//		url2=new TextField("URL TS", Configuration.urlTS, 255, 0);
//		url3=new TextField("URL INTRADAY", Configuration.urlINTRADAY, 255, 0);
//		url4=new TextField("URL FT", Configuration.urlFINANZTREFF, 255, 0);
		
		url5=new TextField("URL IB", Configuration.urlIB, 255, 0);
		link1=new TextField("Link FT", Configuration.link1, 255, 0);
		link2=new TextField("Link OV", Configuration.link2, 255, 0);
//		append(url1);
//		append(url2);
//		append(url3);
//		append(url4);
		append(url5);
		append(link1);
		append(link2);
		
		addCommand(cSave);
		addCommand(cAddLink);
		addCommand(cDelLink);
		addCommand(cCancel);
		setCommandListener(this);
		_main.show(this);		
	}

	public void commandAction(Command command, Displayable dble) {
		if (command==cSave) {
			Configuration.dirtyFlagConfig=true;
			MyLogger.debug("Setting Link settings");
			String[] urls =new String[chartUrls.length];
			for (int i=0; i<chartUrls.length; i++) {
				urls[i]=chartUrls[i].getString();
			}
			Configuration.CHART_URLS=urls;
//			Configuration.urlRT=url1.getString();
//			Configuration.urlTS=url2.getString();
//			Configuration.urlINTRADAY=url3.getString();
//			Configuration.urlFINANZTREFF=url4.getString();
			
			Configuration.urlIB=url5.getString();
			Configuration.link1=link1.getString();
			Configuration.link2=link2.getString();		
			_main.show(_fm.getSettingsMenu());
		} else if (command==cAddLink) {
			FormLinksMaintain addLinkForm = new FormLinksMaintain(_fm.getSettingsMenu(),0);
			RTMidlet.getInstance().show(addLinkForm);
		} else if (command==cDelLink) {
			FormLinksMaintain addLinkForm = new FormLinksMaintain(_fm.getSettingsMenu(),1);
			RTMidlet.getInstance().show(addLinkForm);
		} else if (command == cCancel) {
			RTMidlet.getInstance().show(_fm.getSettingsMenu());
		} 
		
	}

}
